package com.aloda.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.constant.ErrorCode;
import com.aloda.dto.question.QuestionAddDTO;
import com.aloda.dto.question.QuestionContentDTO;
import com.aloda.dto.question.QuestionGenerateDTO;
import com.aloda.exception.BaseException;
import com.aloda.exception.WarnException;
import com.aloda.result.Result;
import com.aloda.service.QuestionService;
import com.aloda.vo.question.QuestionListVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;

/**
 * 用户端题目控制层
 * @author aloda
 * @date 2025-3-20
 */
@Slf4j
@RestController("question-user")
@RequestMapping("/user/question")
public class QuestionController {

    @Resource
    private QuestionService questionService;

    /**
     * 新增/更新题目
     * @return
     */
    @PostMapping
    public Result<Long> addQuestion(@RequestBody QuestionAddDTO questionAddDTO) {
        log.info("用户端新增题目：{}",questionAddDTO);
        if (BeanUtil.isEmpty(questionAddDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(questionService.addQuestionByUser(questionAddDTO));
    }

    /**
     * 根据appId查询题目内容
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<String> getQuestionByAppId(@PathVariable Long id) {
        if (id == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        String jsonString = questionService.getQuestionByAppId(id);
        return Result.success("查询成功",jsonString);
    }

    /**
     * 根据appId查询题目脱敏内容（开始答题的显示内容）
     * @param id
     * @return
     */
    @GetMapping("/safe/{id}")
    public Result<QuestionListVO> getQuestionSafetyInfo(@PathVariable Long id) {
        if (id == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        QuestionListVO questionListVO = questionService.getQuestionSafetyInfoList(id);
        return Result.success(questionListVO);
    }

    /**
     * 客户端AI生成题目
     * @param questionGenerateDTO
     * @return
     */
    @GetMapping("/ai")
    public SseEmitter aiGenerateQuestion(QuestionGenerateDTO questionGenerateDTO) {
        log.info("用户使用AI生成题目：{}",questionGenerateDTO);
        if (BeanUtil.isEmpty(questionGenerateDTO)) {
            throw new WarnException(ErrorCode.PARAMS_ERROR);
        }
        return questionService.aiGenerateQuestion(questionGenerateDTO);
    }

    /**
     * 保存AI生成的题目
     * @param questionAddDTO
     * @return
     */
    @PostMapping("/save")
    public Result saveGenerateQuestionBatch(@RequestBody QuestionAddDTO questionAddDTO) {
        log.info("用户保存AI生成的题目：{}", questionAddDTO);
        if (BeanUtil.isEmpty(questionAddDTO) || questionAddDTO.getQuestionContent().isEmpty()) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(questionService.addGenerateQuestion(questionAddDTO));
    }
}
